﻿#include "httpgetexample.h"

HttpGetExample::HttpGetExample(QObject *parent) : QObject(parent)
{
    // 创建QNetworkAccessManager对象，用于管理网络请求
    manager = new QNetworkAccessManager(this);

    // 连接请求完成信号与槽
    connect(manager, &QNetworkAccessManager::finished, this, &HttpGetExample::onReplyFinished);

    // 设置请求URL
    QUrl url("http://www.baidu.com");
    QNetworkRequest request(url);

    // 发送GET请求
    manager->get(request);
}

void HttpGetExample::onReplyFinished(QNetworkReply *reply)
{
    // 检查是否有错误
    if (reply->error() == QNetworkReply::NoError) {
        // 读取响应的数据
        QByteArray responseData = reply->readAll();
        qDebug() << "Response Data: " << responseData;
    } else {
        // 打印错误信息
        qDebug() << "Error: " << reply->errorString();
    }

    // 释放Reply对象
    reply->deleteLater();
}
